//显示错误信息
function showErrorInfo(msg) {
    layer.msg(msg, {
        shade: 0.3,
        shadeClose: true,
        icon: 5,
        time: 2000 //2秒关闭
    });
}
$(document).ready(function () {

    /* =================================================================== common ===================================================================== */

    //监听form-control的input值改变
    $('input.form-control').bind('input propertychange', function () {
        $(this).next('p').css('display', $(this).val().length < 1 ? 'block' : 'none');
    })

    //form-control失焦
    $('input.form-control').blur(function () {
        $(this).removeClass('input-invalid').next('p').css('display', 'none');
        if ($(this).val() == '') {
            $(this).addClass('input-invalid').next('p').css('display', 'block');
        }
    })

    /* =================================================================== login ===================================================================== */

    //获取cookie，填充表单
    if ($.cookie("remember-login") == "true") {
        $("#remember-login").prop("checked", true);
        $("#login-username").val($.cookie("login-username"));
        $("#login-password").val($.cookie("login-password"));
    }

    //保存表单信息到cookie
    function saveLoginInfoToCookie() {
        if ($('#remember-login').prop('checked') == true) {
            var username = $('#login-username').val();
            var password = $('#login-password').val();
            $.cookie('remember-login', 'true', {expires: 7});
            $.cookie('login-username', username, {expires: 7});
            $.cookie('login-password', password, {expires: 7});
        } else {
            $.cookie('remember-login', 'false', {expires: -1});
            $.cookie('login-username', '', {expires: -1});
            $.cookie('login-password', '', {expires: -1});
        }
    }

    //检查登录表单
    function checkLoginForm() {
        var username = $('#login-username').val();
        var password = $('#login-password').val();
        if (username == null || username == '') {
            $('#login-username').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (password == null || password == '') {
            $('#login-password').next().css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        $('#login-username').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#login-password').next().css('display', 'none').prev().removeClass('input-invalid');
        return true;
    }

    //点击登录按钮
    $('#login').click(function () {
        var pwdHash = $('#login-password').val();
        if (!checkLoginForm()) {
            return;
        }
        else {
            $('#wr-loading-box').removeClass('hidden');
            $.ajax({
                type: 'POST',
                data: {
                    userName: $('#login-username').val(),
                    password: pwdHash
                },
                url: "/api/user/login.html",
                success: function (result) {
                    $('#wr-loading-box').addClass('hidden');
                    if (result.status == 1) {
                        saveLoginInfoToCookie();
                        location.assign("/home.html");
                    } else {
                        if(result.msg == "" || result.msg == null) {
                            showErrorInfo("空，未知错误信息请联系管理员");
                            return;
                        }
                        showErrorInfo(result.msg);
                    }
                },
                error: function () {
                    $('#wr-loading-box').addClass('hidden');
                    showErrorInfo('系统或者网络出错，请联系管理员解决');
                }
            });
        }
    });

    /* =================================================================== signUp ===================================================================== */

    //检查注册表单
    function checkSignForm() {
        var username = $('#sign-username').val();
        var password = $('#sign-password').val();
        var confirm = $('#sign-confirm').val();
        var email = $('#sign-email').val();
        var signature = $('#sign-signature').val();
        var expectedWeight = $('#sign-ep-weight').val();
        var checkbox = $('#agree-sign');
        var emailReg = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/;
        var usernameReg = /^[a-zA-Z\d]\w{4,14}[a-zA-Z\d]$/;
        if (username == null || username == '') {
            $('#sign-username').next().html("请输入用户名").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (!usernameReg.test(username)) {
            $('#sign-username').next().html("应是大小写字母、数字或下划线且长度在6~16位").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (password == null || password == '') {
            $('#sign-password').next().html("请输入密码").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (confirm == null || confirm == '') {
            $('#sign-confirm').next().html("请确认密码").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (confirm != password) {
            $('#sign-confirm').next().html("两次密码不一致").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (email == null || email == '') {
            $('#sign-email').next().html("请输入邮箱").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (!emailReg.test(email)) {
            $('#sign-email').next().html("邮箱格式不正确").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (signature == null || signature == '') {
            $('#sign-signature').next().html("请输入塑形宣言").css('display', 'block').prev().addClass('input-invalid');
            return false;
        }
        if (expectedWeight == null || expectedWeight == '') {
            return false;
        }
        if (!checkbox.prop("checked")) {
            $('#agree-sign').next().next().css('display', 'block');
            return false;
        }
        $('#sign-username').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#sign-password').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#sign-confirm').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#sign-email').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#sign-signature').next().css('display', 'none').prev().removeClass('input-invalid');
        $('#sign-ep-weight').removeClass('input-invalid');
        $('#agree-sign').next().next().css('display', 'none');
        return true;
    }

    //同意申明
    $('#agree-sign').change(function () {
        $(this).next().next().css('display', $(this).prop('checked') ? 'none' : 'block');
    });

    //点击同意协议
    $('#btn-accept').click(function () {
        if (!$('#agree-sign').prop('checked')) {
            $('#agree-sign').click();
        }
    });

    //点击注册按钮
    $('#sign').click(function () {
        if (!checkSignForm()) {
            return;
        }
        else {
            $('#wr-loading-box').removeClass('hidden');
            $.ajax({
                type: 'POST',
                data: {
                    userName: $('#sign-username').val(),
                    sex: $('#sign-sex').val(),
                    password: hex_sha1($('#sign-password').val() + "{" + $('#sign-username').val() + "}"),
                    expectedWeight: $('#sign-ep-weight').val(),
                    signature: $('#sign-signature').val().replace(/<\/?[^>]*>/g, ''), //去掉html标签
                    email: $('#sign-email').val()
                },
                url: "/api/user/adduser.html",
                success: function (result) {
                    $('#wr-loading-box').addClass('hidden');
                    if (result.status == 1) {
                        layer.msg("注册成功，2s后跳转至登录页...", {
                            shade: 0.3,
                            shadeClose: true,
                            icon: 6,
                            time: 2000 //2秒关闭
                        }, function () {
                            location.assign("/index.html");
                        });
                    } else {
                        $('#wr-loading-box').addClass('hidden');
                        showErrorInfo(result.msg)
                    }
                },
                error: function () {
                    $('#wr-loading-box').addClass('hidden');
                    showErrorInfo('系统或者网络出错，请联系管理员解决');
                }
            })
        }
    });

    /* =================================================================== openAccount ===================================================================== */

    //点击协议弹出modal
    $('.openAcc-protocol').click(function () {
        $('#protocolModal').modal('show');
    });
})